var express = require('express');
var router = express.Router();
var dbaUtils = require('../common/dba');


/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express111' });
});



//增
router.get('/addUser', function (req, res, next) {
    var sql = 'insert into sys_user (`username` ,`password`) values ("' + req.query.username + '","' + req.query.password + '")';
    dbaUtils.handleDba(sql,req.headers.authorization,(result)=>{
        res.json({result: result})
    })
});
router.post('/addUser', function (req, res, next) {
    console.log('addPostUser:---------' + req.body.username)
    var sql = 'insert into sys_user (`username` ,`password`) values ("' + req.body.username + '","' + req.body.password + '")';
    dbaUtils.handleDba(sql,req.headers.authorization,(result)=>{
        res.json({result: result})
    })
});

//删
router.post('/deleteUser', function (req, res, next) {
    console.log('delete:---------' + req.body.userId)
    var sql = 'delete from sys_user where user_id=' + req.body.userId;
    mysql.query(sql, function (err, result) {
        res.json({result: result})
    });
});

//改
router.post('/modifyUser', function (req, res, next) {
    var sql = 'insert into sys_user (`username` ,`password`) values ("' + req.body.username + '","' + req.body.password + '")';
    mysql.query(sql, function (err, result) {
        res.json({result: result})
    });
});

//查
router.get('/selectUser', function (req, res, next) {
    var sql = 'SELECT * FROM sys_user';
    dbaUtils.handleDba(sql,req.headers.authorization,(result)=>{
        if (result.length > 0) {
            res.json({
                result:
                    {
                        type: 'get',
                        res: result,
                    }
            })
        }
        else {
            res.json({result: 'none'})
        }
    })
});

router.post('/selectUser', function (req, res, next) {
    var sql = 'SELECT * FROM sys_user';
    dbaUtils.handleDba(sql,req.headers.authorization,(result)=>{
        if (result.length > 0) {
            res.json({
                result:
                    {
                        type: 'get',
                        res: result,
                    }
            })
        }
        else {
            res.json({result: 'none'})
        }
    })
});



module.exports = router;
